package logic

import (
	"context"
	"errors"
	"we-chat-mgr/models/mysql"
	"we-chat-mgr/pkg"

	"we-chat-mgr/wechatrpc/internal/svc"
	"we-chat-mgr/wechatrpc/wechatrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *wechatrpc.LoginRequest) (*wechatrpc.LoginResponse, error) {
	// todo: add your logic here and delete this line

	//用户登录 用户存不存在->手机号有没有注册账号 查id是否为0 判断密码是否正确
	//验证码 短信 + 图形
	var findUser mysql.User
	err := findUser.FindUserByTel(mysql.DB, in.Tel)
	if err != nil {
		return nil, err
	}
	if findUser.ID == 0 {
		return nil, errors.New("该手机号未注册,请重试")
	}
	if pkg.Md5(in.Password) != findUser.Password {
		return nil, errors.New("密码错误,请重试")
	}

	return &wechatrpc.LoginResponse{
		UserId: int64(findUser.ID),
	}, nil
}
